package com.ysd.gulimall.cart.controller;

import com.ysd.common.vo.MemberRespVo;
import com.ysd.gulimall.cart.feign.ProductFeignService;
import com.ysd.gulimall.cart.interceptor.CartInterceptor;
import com.ysd.gulimall.cart.service.CartService;
import com.ysd.gulimall.cart.vo.Cart;
import com.ysd.gulimall.cart.vo.CartItem;
import com.ysd.gulimall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller

public class CartController {
    @Autowired

    CartService cartService;
    @GetMapping("/deleteItem")
    @ResponseBody
    public Integer deleteItem(@RequestParam("deleteId") Long deleteId){
        cartService.deleteItem(deleteId);
      //  return "redirect:http://localhost:30000/cart.html";
        return 1;
    }


    @GetMapping("/countItem")
    @ResponseBody
    public Integer countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num){
        cartService.changeItemCount(skuId,num);
      //  return "redirect:http://localhost:30000/cart.html";
        return 1;
    }

    @GetMapping("/checkItem")
    @ResponseBody
    public Integer checkItem(@RequestParam("skuId") Long skuId,@RequestParam("check") Integer check){
        cartService.checkItem(skuId,check);
        return 1;
        //return "redirect:http://localhost:30000/cart.html";
    }



    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItems(){

        return cartService.getUserCartItems();
    }

    @GetMapping("/cart.html")
    public String cartListPage(HttpSession session,Model model) throws ExecutionException, InterruptedException {

        //快速得到用户信息  userId userKey
        //ThreadLocal 同一个线程共享数据

        //System.out.println(userInfoTo);
        Cart cart=cartService.getCart();
        MemberRespVo member = (MemberRespVo)session.getAttribute("loginUser");
        if(member!=null){
            model.addAttribute("nickName",member.getNickname());
        }else{
            model.addAttribute("nickName",null);
        }
        model.addAttribute("cart",cart);
        return "cartList";
    }
    //添加商品到购物车
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes ra) throws ExecutionException, InterruptedException {

        cartService.addToCart(skuId,num);
        ra.addAttribute("skuId",skuId);
        return "redirect:http://localhost:30000/addToCartSuccess.html";
    }
    //跳转到成功页面，避免刷新页面重复添加
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model,HttpSession session){
        //重定向成功页面，再次查询购物车数据
        MemberRespVo member = (MemberRespVo)session.getAttribute("loginUser");
        if(member!=null){
            model.addAttribute("nickName",member.getNickname());
        }else{
            model.addAttribute("nickName",null);
        }
        CartItem item= cartService.getCartItem(skuId);
        model.addAttribute("item",item);
        return "success";
    }
    
}
